#!/usr/bin/env python
# Create: 2019/1/8
__author__ = '749B'
"""基本使用"""
from twisted.web.client import getPage, defer
from twisted.internet import reactor


# 所有任务完成后的回调函数
def all_done(arg):
    """所有爬虫执行完后执行，循环终止"""
    print("All Done")
    reactor.stop()


# 单个任务的回调函数
def callback(contents):
    """每个爬虫获取到结果后执行"""
    print(contents)


deferred_list = []

url_list = [
    'http://www.bing.com',
    'http://www.baidu.com',
    'http://edu.51cto.com',
]

for url in url_list:
    deferred = getPage(bytes(url, encoding='utf-8'))
    deferred.addCallback(callback)
    deferred_list.append(deferred)

dlist = defer.DeferredList(deferred_list)
dlist.addBoth(all_done)

if __name__ == '__main__':
    reactor.run()
